package group.nft.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginTimeInterceptor extends HandlerInterceptorAdapter {
	private int startTime;
	private int endTime;

	// 依赖注入,请看配置文件
	public void setStartTime(int startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(int endTime) {
		this.endTime = endTime;
	}

	// 在控制器执行前调用
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		if (String.valueOf(request.getRequestURL()).endsWith("login") || session.getAttribute("account") != null) {
			return true;
		}
		// 没有通过拦截器，返回登录页面
		response.sendRedirect(request.getContextPath() + "index/login.html");
		return false;
		// }
	}

	// 在后端控制器执行后调用
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		super.postHandle(request, response, handler, modelAndView);
	}

	// 整个请求执行完成后调用
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}